package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.domain.FreezeInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ManageService {
    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private FreezeInfoApi freezeInfoApi;

    //用户数据翻页
    public PageResult users(Integer page, Integer pagesize) {
        IPage iPage = userInfoApi.findAll(page, pagesize);
        return new PageResult(page, pagesize, iPage.getTotal(), iPage.getRecords());
    }

    //查询用户详情
    public UserInfo findUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        return userInfo;
    }

    //查询视频列表
    public PageResult findVideos(Integer page, Integer pagesize, Long uid) {
        //1. 调用videoApi方法，查询视频信息
        PageResult pageResult = videoApi.findByUserId(uid, page, pagesize);
        //2. 获取pageResult里的list
        List<Video> items = (List<Video>) pageResult.getItems();
        //3. 判断是否为空
        if (CollUtil.isEmpty(items)) {
            return pageResult;
        }
        //4. 不为空，就将items集合中的video替换成videoVo，再进行保存
        List<Long> ids = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids);
        List<VideoVo> vos = new ArrayList<>();
        for (Video video : items) {
            UserInfo userInfo = map.get(video.getUserId());
            VideoVo vo = VideoVo.init(userInfo, video);
            vos.add(vo);
        }
        //5. 返回
        pageResult.setItems(vos);
        return pageResult;
    }

    //查询动态列表
    public PageResult findMovement(Integer page, Integer pagesize, Long uid, Integer states) {
        //1. 调用movementApi方法查询PageResult对象
        PageResult result = movementApi.findMovement(page, pagesize, uid, states);
        //2. 在result中获取Movement的集合
        List<Movement> items = (List<Movement>) result.getItems();
        if (CollUtil.isEmpty(items)) {
            return result;
        }
        //3. 将Movement的集合，替换成MovementVO
        List<Long> ids = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids);
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : items) {
            UserInfo userInfo = map.get(movement.getUserId());
            MovementsVo vo = MovementsVo.init(userInfo, movement);
            vos.add(vo);
        }
        result.setItems(vos);
        //4. 返回
        return result;
    }

    //根据消息id查询
    public MovementsVo findMovementVo(String id) {
        Movement movement = movementApi.findById(id);
        UserInfo userInfo = userInfoApi.findById(movement.getUserId());
        MovementsVo vo = MovementsVo.init(userInfo, movement);
        return vo;
    }


    //获取评论列表
    public PageResult findComments(String id, Integer page, Integer pagesize) {
        Integer commentType = 2;
        List<Comment> comments = commentApi.findComments(id, commentType, page, pagesize);
        if (CollUtil.isEmpty(comments)) {
            return new PageResult(page, pagesize, 0l, comments);
        }
        List<Long> ids = CollUtil.getFieldValues(comments, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids);
        Long count = commentApi.findCount(id, commentType);
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = map.get(comment.getUserId());
            CommentVo vo = CommentVo.init(userInfo, comment);
            vos.add(vo);
        }
        return new PageResult(page, pagesize, count, vos);
    }

    //用户冻结--保存冻结信息--并将用户状态到用户信息表中
    public void save(FreezeInfo freezeInfo) {
        freezeInfoApi.insert(freezeInfo);

        //将用户状态到用户信息表中
        String userStatus = "2";
        UserInfo userInfo = new UserInfo();
        userInfo.setId(Long.valueOf(freezeInfo.getUserId()));
        userInfo.setUserStatus(userStatus);
        userInfoApi.update(userInfo);
    }

    //用户解冻--
    public void unfreeze(Integer userId, String reasonsForThawing) {
        //1. 将用户信息表中的用户状态修改过来,并保存
        UserInfo userInfo = userInfoApi.findById(Long.valueOf(userId));
        userInfo.setUserStatus("1");
        userInfoApi.update(userInfo);
        //2. 将冻结信息表中的数据删除
        freezeInfoApi.deleteByUserId(userId);

    }
}
